package cn.neusoft.service.impl;


import cn.neusoft.mapper.AddressMapper;
import cn.neusoft.pojo.Address;
import cn.neusoft.query.AddressQuery;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IAddressService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class AddressService implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Transactional
    public ResultBean add(Address address) throws Exception {
        if(address==null || StringUtils.isEmpty(address.getContactName())
            || StringUtils.isEmpty(address.getAddress())) {
            return ResultBean.ERROR("请求参数为空");
        }
        int count= addressMapper.add(address);
        return count>0?ResultBean.SUCCESS(address.getAddressId()):ResultBean.ERROR("add is failed");
    }
    @Transactional
    public ResultBean update(Address address) throws Exception {
        if(address==null || StringUtils.isEmpty(address.getContactName())
                || StringUtils.isEmpty(address.getAddress()) ||
                    address.getAddressId()==null || address.getUserId()==0) {
            return ResultBean.ERROR("请求参数为空");
        }
        int count= addressMapper.update(address);
        return count>0?ResultBean.SUCCESS():ResultBean.ERROR("update is failed");
    }
@Transactional
    public ResultBean delete(Integer id) throws Exception {
        if(id==null) {
            return ResultBean.ERROR("请求参数为空");
        }
        int count= addressMapper.delete(id);
        return count>0?ResultBean.SUCCESS():ResultBean.ERROR("delete is failed");
    }
    @Transactional(readOnly = true)
    public ResultBean getById(Integer id) throws Exception {
        if(id==null) {
            return ResultBean.ERROR("请求参数为空");
        }
        Address address = addressMapper.getById(id);
        return address!=null?ResultBean.SUCCESS(address):ResultBean.ERROR("getById is failed");
    }
    @Transactional(readOnly = true)
    public ResultBean getAll() throws Exception {
       List<Address> list=addressMapper.getAll();
       return ResultBean.SUCCESS(list);
    }
    @Transactional(readOnly = true)
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
        PageBean<Address> pageBean=new PageBean<Address>();
        if(baseQuery==null) {
            baseQuery=new AddressQuery();
        }
        int count=addressMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        List<Address> list=addressMapper.getByPage(baseQuery);
        pageBean.setData(list);
        return ResultBean.SUCCESS(pageBean);
    }
@Transactional
    public ResultBean batchDel(List<Integer> ids) throws Exception {
        if(ids==null) {
            return ResultBean.ERROR("请求参数为空");
        }
        for (Integer len : ids ) {
            int delete = addressMapper.delete(len);
            if(delete<0) {
                System.out.println("删除商家失败");
            }
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean getByUserId(Integer userId) {
        if(userId==null || userId==0) {
            return ResultBean.ERROR("请求参数为空");
        }
        List<Address> list=addressMapper.getByUserId(userId);
        return ResultBean.SUCCESS(list);
    }
}
